package com.yingxin.yxerp.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yxerp.module.sys.entity.BankAccount;
import com.yingxin.yxerp.module.sys.mapper.BankAccountMapper;
import com.yingxin.yxerp.module.sys.service.IBankAccountService;
import com.yingxin.yxerp.module.sys.vo.req.BankAccountQueryVo;
import com.yingxin.yxerp.module.sys.vo.resp.BankAccountRespVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 银行账户 服务实现类
 * @author fanchao
 * @since 2021-08-18
 */

@Service
public class BankAccountServiceImpl
        extends ServiceImpl<BankAccountMapper, BankAccount>
        implements IBankAccountService {

    /**
     * 分页查询
     */
    @Override
    public IPage<BankAccountRespVo> pageVo(Page page, BankAccountQueryVo queryVo) {
        return baseMapper.pageVo(page, queryVo);
    }

    /**
     * 查询全部
     */
    @Override
    public List<BankAccountRespVo> listVo(BankAccountQueryVo queryVo) {
        return baseMapper.listVo(queryVo);
    }

    /**
     * 根据id查询
     */
    @Override
    public BankAccountRespVo getVo(String id) {
        return baseMapper.getVo(id);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(BankAccount account) {
        checkAccount(account, null);
        save(account);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void update(BankAccount account) {
        checkAccount(account, account.getId());
        updateById(account);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void delete(BankAccount account) {
        removeById(account);
    }

    public void checkAccount(BankAccount entity, String id) {
        QueryWrapper<BankAccount> qw = new QueryWrapper<>();
        qw.lambda().eq(BankAccount::getAccountName, entity.getAccountName())
        .eq(BankAccount::getAccountBank,entity.getAccountBank())
        .eq(BankAccount::getAccountNo,entity.getAccountNo());
        BankAccount temp = getOne(qw);
        if (null != temp && !temp.getId().equals(id)) {
            throw new BizException("开户行、开户名、银行账号不能重复!");
        }
    }

}
